package com.hps.spring.cloud.initializrstart.service.impl;

import com.hps.spring.cloud.initializrstart.service.CityDataService;
import com.hps.spring.cloud.initializrstart.util.XmlBuilder;
import com.hps.spring.cloud.initializrstart.vo.City;
import com.hps.spring.cloud.initializrstart.vo.CityList;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Service;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.List;

/**
 * @Author: hps
 * @Description:
 * @Date: Created in 11:41 2018/8/31
 */
@Service
public class CityDataServiceImpl implements CityDataService {

    @Override
    public List<City> listCity() throws Exception {
        Resource resource = new ClassPathResource("city.xml");
        BufferedReader br = new BufferedReader(new InputStreamReader(resource.getInputStream(), "UTF-8"));
        StringBuffer buffer = new StringBuffer();
        String line = "";
        while ((line = br.readLine()) != null) {
            buffer.append(line);
        }
        br.close();
        CityList cityList = (CityList) XmlBuilder.xmlStrToObject(CityList.class, buffer.toString());
        return cityList.getCityList();
    }
}
